import pandas as pd
import numpy as np


def dateBaseFun():
    """
    DataFrame str转换datetime、转换年、转换月、转换日、转换分钟、转换秒、转换星期、转换季度、判断是否年底
    :return:
    """
    # 解决数据输出时列名不对齐的问题
    pd.set_option('display.unicode.ambiguous_as_wide', True)
    pd.set_option('display.unicode.east_asian_width', True)

    date_df = pd.DataFrame(data=['10-Feb-20', '10/14/2021', '2022.04.10', '2023/04/10', '20231231121212'],
                           columns=["日期(str)"])
    date_df["日期(datetime)"] = pd.to_datetime(date_df["日期(str)"])
    date_df["年"] = date_df["日期(datetime)"].dt.year
    date_df["月"] = date_df["日期(datetime)"].dt.month
    date_df["日"] = date_df["日期(datetime)"].dt.day
    date_df["时"] = date_df["日期(datetime)"].dt.hour
    date_df["分"] = date_df["日期(datetime)"].dt.minute
    date_df["秒"] = date_df["日期(datetime)"].dt.second
    date_df["星期"] = date_df["日期(datetime)"].dt.day_name()
    date_df["季度"] = date_df["日期(datetime)"].dt.quarter
    date_df["年底"] = date_df["日期(datetime)"].dt.is_year_end
    date_df["季度底"] = date_df["日期(datetime)"].dt.is_quarter_end
    date_df["月底"] = date_df["日期(datetime)"].dt.is_month_end

    print(date_df)


def dateSortFun():
    """
    日期排序 升序
    :return:
    """
    date = pd.DataFrame(data=[pd.to_datetime("2023-01-01"), pd.to_datetime("2022-01-01"), pd.to_datetime("2023-12-01")],
                        columns=["日期"])
    date_df = date.sort_values("日期")
    print(date_df)


def dateFilterFun():
    """
    日期过滤
    :return:
    """
    date = pd.DataFrame(
        data=[
            [pd.to_datetime("2021-01-01"), 1],
            [pd.to_datetime("2022-01-01"), 1],
            [pd.to_datetime("2022-12-01"), 1],
            [pd.to_datetime("2023-06-01"), 1],
            [pd.to_datetime("2023-06-02"), 1],
            [pd.to_datetime("2023-07-02"), 1],
            [pd.to_datetime("2023-12-01"), 1]
        ],
        columns=["日期", "数量"])

    date = date.set_index("日期")

    date_filter_1 = date['2022']
    date_filter_2 = date['2021':'2022']
    date_filter_3 = date['2021-01-01':'2022-12-01']

    print(date_filter_1)
    print(date_filter_2)
    print(date_filter_3)


def datAggFun():
    """
    日期过滤
    :return:
    """
    date = pd.DataFrame(
        data=[
            [pd.to_datetime("2021-01-01"), 1],
            [pd.to_datetime("2022-01-01"), 1],
            [pd.to_datetime("2022-12-01"), 1],
            [pd.to_datetime("2023-06-01"), 1],
            [pd.to_datetime("2023-06-02"), 1],
            [pd.to_datetime("2023-07-02"), 1],
            [pd.to_datetime("2023-12-01"), 1]
        ],
        columns=["日期", "数量"])

    date = date.set_index("日期")

    group_year = date.resample("AS").sum().to_period("A")
    print(group_year)

    group_quarter = date.resample("Q").sum().to_period("Q")
    print(group_quarter)

    group_month = date.resample("M").sum().to_period("M")
    print(group_month)

    group_week = date.resample("W").sum().to_period("W")
    print(group_week)

    group_day = date.resample("D").sum().to_period("D")
    print(group_day)


if __name__ == '__main__':
    datAggFun()
    # dateFilterFun()
    # dateSortFun()
    # dateBaseFun()
